<SCRIPT type="text/javascript" src="/view/javascripts/form.js"></SCRIPT>
<SCRIPT type="text/javascript">
function expandit(id){
    obj = document.getElementById(id);
    if (obj.style.display=="none") obj.style.display="";
    else obj.style.display="none";
  }
</SCRIPT>

<DIV class="block">
	<H1><?php echo $response->getParameter('title');?></H1>
<?php
	if($response->isParameter('types')) {
?>
	<UL class="tabs">
<?php
		foreach($response->getParameter('types') as $type) {
			$id = $type->getIdentity();
			$name = $type->getName();
			if($id === $response->getParameter('type')->getIdentity()) {
?>
		<LI class="active"><STRONG><?php echo $name;?></STRONG></LI>
<?php
			} else {
?>
		<LI class="inactive">
			<FORM class="hidden" id="form-<?php echo $id;?>" action="index.html" method="post">
				<INPUT type="hidden" name="identity" value="<?php echo $id;?>">
			</FORM>
			<A href="#" onclick="submitForm('form-<?php echo $id;?>')"><?php echo $name;?></A>
		</LI>
<?php
			}
		}
?>
		<LI class="inactive">
		<A href="/monitoring/indicator/indicatorAvgVal.html" >Университет</A></LI>
	</UL>
<?php
	}
?>
</DIV>
<DIV id="year">
<?php
	$year = (int)unserialize($_SESSION['year']);
?>
	<FORM action="index.html" method="post">
<?php
	if($response->isParameter('type')) {
?>
		<INPUT type="hidden" name="identity" value="<?php echo $response->getParameter('type')->getIdentity();?>">
<?php
	}
?>
		<INPUT type="hidden" name="year" value="<?php echo $year-1;?>">
		<INPUT type="submit" value="&lt;&lt;&lt;">
	</FORM>
<?php
	echo $year;
	if ($year < date("Y")) {
?>
	<FORM action="index.html" method="post">
<?php
	if($response->isParameter('type')) {
?>
		<INPUT type="hidden" name="identity" value="<?php echo $response->getParameter('type')->getIdentity();?>">
<?php
	}
?>
		<INPUT type="hidden" name="year" value="<?php echo $year+1;?>">
		<INPUT type="submit"  value="&gt;&gt;&gt;">
	</FORM>
<?php
	}
?>
</DIV>
<DIV class="block">
<?php
	if($response->isParameter('quarte')) {
?>
	<FORM action="/editing/division/update.html" method="post">
<?php
	}
?>
	<TABLE>
			<TR>
			<TH rowspan="3">Процесс</TH>
			<TH rowspan="3">Владелец</TH>
			<TH rowspan="3">Цель</TH>
			<TH rowspan="3">Показатель</TH>
			<TH rowspan="3">Весомость</TH>
			<TH rowspan="3">Плановое значение</TH>
			<TH colspan="8">Квартал календарного года</TH>
		</TR>
		<TR>
			<TH colspan="2">I</TH>
			<TH colspan="2">II</TH>
			<TH colspan="2">III</TH>
			<TH colspan="2">IV</TH>
		</TR>
		<TR>
			<TH>план</TH>
			<TH>факт</TH>
			<TH>план</TH>
			<TH>факт</TH>
			<TH>план</TH>
			<TH>факт</TH>
			<TH>план</TH>
			<TH>факт</TH>
		</TR>
		
<?php
$responsibilities = $response->getParameter('responsibilities');
$indicatorValues = $response->getParameter('indicatorValues');
foreach($response->getParameter('departments') as $department) {
	$id = $department->getIdentity();
	$name = $department->getName();
?>
		<TR onclick="expandit('department<?php echo $id; ?>');"><TD colspan="14"><STRONG><?php echo $name;?></STRONG></TD></TR>
	<TBODY id="department<?php echo $id; ?>" style="display: none">
<?php
$j=0;
$count=array();//количество повторений данного процесса
$count[]=1;
for ($i = 1; $i <sizeof($responsibilities[$id]); $i++) {
	if ($responsibilities[$id][$i]->getIndicator()->getProcess()->getIdentity() ==$responsibilities[$id][$i-1]->getIndicator()->getProcess()->getIdentity()) {
		$count[$j]++;
	}else {
		$j++;
		$count[]=1;
	}
}
$j=0;
$k=1;
for ($i = 0; $i <sizeof($responsibilities[$id]); $i++) {
	//если строка первая или единственная в блоке, то выводим полностью
	if($k==1){
		?>
		<TR >
			<TD <?php if ($count[$j]>1) {
				echo "rowspan=\"$count[$j]\"";
			}?>><?php echo $responsibilities[$id][$i]->getIndicator()->getProcess()->getName();?></TD>
			<TD <?php if ($count[$j]>1) {
				echo "rowspan=\"$count[$j]\"";
			}?>><?php echo $responsibilities[$id][$i]->getIndicator()->getProcess()->getOwner()->getName();?></TD>
			<TD <?php if ($count[$j]>1) {
				echo "rowspan=\"$count[$j]\"";
			}?>><?php echo $responsibilities[$id][$i]->getIndicator()->getProcess()->getObject();?></TD>
			<TD><?php echo $responsibilities[$id][$i]->getIndicator()->getName();?></TD>
			<TD><?php echo $responsibilities[$id][$i]->getIndicator()->getWeight();?></TD>
			<TD><?php if ($responsibilities[$id][$i]->getIndicator()->getPlanValue()!=null){
					echo $responsibilities[$id][$i]->getIndicator()->getPlanValue();}
					else echo $responsibilities[$id][$i]->getPlanValue();?>
					</TD>
			<?php for ($q = 1; $q <= 4 ; $q++) {
				$str="white";
				if (isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
					if ($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getPlanValue()>
					$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getValue()) {
						$str="magenta";
					}
				}
				//плановое значение
				echo "<TD bgcolor=".$str.">";
				if($response->isParameter('quarte') && ($q-1 == $response->getParameter('quarte') || ($q==1 && $response->getParameter('quarte')==1))) {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
						echo '<INPUT name="indicatorPlan-'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getIdentity().'" ';
						echo 'value="'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getPlanValue().'">';
					} else {
						echo "-";
					}
				} else {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])){
 						echo $indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getPlanValue();
 					} else {
 						echo "-";
 					}
				}
				echo"</TD>";
				//фактическое значение
				echo "<TD bgcolor=".$str.">";
				if($response->isParameter('quarte') && $q == $response->getParameter('quarte')) {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
						echo '<INPUT name="indicator-'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getIdentity().'" ';
						echo 'value="'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getValue().'">';
					} else {
						echo "-";
					}
				} else {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
						echo $indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getValue();
					} else {
						echo "-";
					}
				}
				echo "</TD>";
			} ?>
			
		</TR>
		<?php
		}else {
			?><TR>
				<TD><?php echo $responsibilities[$id][$i]->getIndicator()->getName();?></TD>
				<TD><?php echo $responsibilities[$id][$i]->getIndicator()->getWeight();?></TD>
				<TD><?php if ($responsibilities[$id][$i]->getIndicator()->getPlanValue()!=null){
					echo $responsibilities[$id][$i]->getIndicator()->getPlanValue();}
					else echo $responsibilities[$id][$i]->getPlanValue();?></TD>
					
			<?php for ($q = 1; $q <= 4 ; $q++) {
				$str="white";
				if (isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
					
					if ($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getPlanValue()>
					$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getValue()) {
						$str="magenta";
					}
				}
				
				echo "<TD bgcolor=".$str.">";
				if($response->isParameter('quarte') && ($q-1 == $response->getParameter('quarte')|| ($q==1 && $response->getParameter('quarte')==1) )) {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
						echo '<INPUT name="indicatorPlan-'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getIdentity().'" ';
						echo 'value="'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getPlanValue().'">';
					} else {
						echo "-";
					}
				}
				else{
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])){
						echo $indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getPlanValue();
						
					}else echo "-";
				}
				echo"</TD>";
				echo "<TD bgcolor=".$str.">";
				if($response->isParameter('quarte') && $q == $response->getParameter('quarte')) {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
						echo '<INPUT name="indicator-'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getIdentity().'" ';
						echo 'value="'.$indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getValue().'">';
					} else {
						echo "-";
					}
				} else {
					if(isset($indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q])) {
						echo $indicatorValues[$responsibilities[$id][$i]->getIdentity()][$q]->getValue();
					} else {
						echo "-";
					}
				}
			echo "</TD>";
			} ?>
				</TR>
			<?php 
		}
		if($count[$j]==$k){
			$j++;
			$k=1;
		}else {
			$k++;
		}
		}//for responsibility
?></TBODY><?php }//foreach department
?>
	</TABLE>
<?php
	if($response->isParameter('quarte')) {
?>
		<BUTTON type="submit">
				<IMG src="/view/img/button-save.png">
				<SPAN>сохранить</SPAN>
		</BUTTON>
	</FORM>
<?php
	}
?>
</DIV>
